// pages/cardverfy/index.js
const { Http } = require("../../utils/http");
import { config } from "../../config/config";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scene: '',
    result: [],//选中的值
    resList: [],//得到的权益数组
    right: [],//提交的数据
    poID: []//当前推销员所在门店中的ID
  },

  onChange(event) {//得到选中的值
    this.setData({
      result: event.detail
    });
  },
  
  //生命周期函数--监听页面加载
  onLoad: function (options) {

    this.setData({ scene: options.scene })
    console.log(options.scene);
    this.getToken();
  },

  getToken: function () {//wechat/index/login
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        wx.request({
          url: `${config.apiBaseUrl}wechat/index/login`,
          data: {
            code: res.code,
          },
          success: (res) => {
            wx.setStorageSync('token', res.data.data.token);
            this.getinit();
            this.getCard();
          }
        });
      }
    })
  },

  getinit: async function () {
    let result = await Http.request({ //获取 默认
      url: 'wechat/index/init',
      data: { token: wx.getStorageSync('token') },
      method: "POST"
    });
    if (result.code == 0) {

      this.setData({
        poID: result.data.userinfo.store.category_ids.split( ',' )
      })
      // console.log(this.data.poID);
    }
    let s= 24;
    // console.log(this.data.poID.indexOf(s.toString()) != -1);
    // console.log(this.data.poID.indexOf(23) != -1);
  },

  getCard: async function () {
    let result = await Http.request({ //
      url: 'wechat/card/get_card_detail_by_id',
      data: { token: wx.getStorageSync('token'), id: this.data.scene },
      method: "POST"
    });
    console.log(result.msg);
    if (result.code == 0) {
      // 循环向对象中添加默认值
      for (let index = 0; index < result.data.order_right.length; index++) {
        result.data.order_right[index]['nums'] = 1;
      }
      this.setData({
        resList: result.data.order_right
      })
      // console.log(this.data.right);
    } else {
      console.log(result);
    }
  },

  onClickButton: function () { //选择核销的内容
    let that = this;
    that.data.right = [];
    for (let x = 0; x < that.data.result.length; x++) {
      for (let y = 0; y < that.data.resList.length; y++) {
        if (that.data.result[x] == that.data.resList[y].id) {
          console.log(that.data.resList[y].id);
          var vote = {};
          vote.id = that.data.resList[y].id;
          vote.count = that.data.resList[y].nums;
          vote.store_id = that.data.resList[y].store_id;
          that.data.right.push(vote);
        }
      }
    }
    if (that.data.right.length == 0) {
      wx.showToast({
        title: '请选择核销的权益卡',
        duration: 2000,
        icon: 'none'
      });
    } else {
      console.log(that.data.right);
      wx.showModal({
        title: '提示',
        content: '是否确定核销',
        success(res) {
          if (res.confirm) {
            that.onVief(that.data.right);
            // 
            wx.redirectTo({
              url: '/pages/verifyman/index',
            })
          } else if (res.cancel) {
            console.log('用户点击取消');
          }
        }
      })
    }
  },
  onVief: async function (Dete) { //核销接口
    let result = await Http.request({
      url: 'wechat/order_virtual/verify',
      data: { right: Dete },
      method: "POST"
    });
    wx.showToast({
      title: result.msg,
      duration: 2000,
      icon: 'none'
    });
  },

  //点击减少时执行的函数
  red: function (e) {
    const index = e.currentTarget.dataset.index;
    let carts = this.data.resList;
    let num = carts[index].nums;
    if (num <= 1) {
      return
    }
    num = num - 1;
    carts[index].nums = num;
    this.setData({
      resList: carts
    })
    console.log(num);
  },
  //点击加号
  add(e) {
    const count = e.currentTarget.dataset.count;
    const index = e.currentTarget.dataset.index;
    let carts = this.data.resList;
    let num = carts[index].nums;
    if (num >= count) {
      return
    }
    num = num + 1;
    carts[index].nums = num;
    this.setData({
      resList: carts
    })
  },
  //监听 输入框中是否是数字 还是空
  mobileInput(e) {
    const count = e.currentTarget.dataset.count;
    const index = e.currentTarget.dataset.index;
    let value = e.detail.value.replace(/\D/g, '');
    // console.log(value+'+'+count);
    if (typeof (e.detail.value) !== 'number') { //不是数字时 把当前值转化为数字
      this.data.resList[index].nums = parseInt(value);
      let carts = this.data.resList;
      this.setData({
        resList: carts
      })
      if (value > count) {//当 转化为的数字大于  最大次数时   把当前数字替换为 最大值
        this.data.resList[index].nums = count;
        let carts = this.data.resList;
        this.setData({
          resList: carts
        })
      }
      // else if(value == null || value == ''|| value == undefined){
      //   this.data.resList[index].nums = 1;
      //   let carts = this.data.resList;
      //   this.setData({
      //     resList: carts
      //   })
      // }
    }
  },
  // input框失去焦点时  是否为空  如果为空 赋值最小值 为1
  bock(e) {
    const count = e.currentTarget.dataset.count;
    const index = e.currentTarget.dataset.index;
    let value = e.detail.value.replace(/\D/g, '');
    if (value == null || value == '' || value == undefined) {
      this.data.resList[index].nums = 1;
      let carts = this.data.resList;
      this.setData({
        resList: carts
      })
    }
  },
  //无法返回上一级
  onShow: function () {
    wx.hideHomeButton()
  },
})